﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using MVVMLightDemo.Models;
using MVVMLightDemo.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MVVMLightDemo.ViewModel
{
    public class MainWindowViewModel:ViewModelBase
    {
        private int count;

        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                this.RaisePropertyChanged("Count");
            }
        }

        private double fee;

        public double Fee
        {
            get { return fee; }
            set
            {
                fee = value;
                this.RaisePropertyChanged("Fee");
            }
        }

        private Restaurant restaurant;

        public Restaurant Restaurant
        {
            get { return restaurant; }
            set
            {
                restaurant = value;
                this.RaisePropertyChanged("Restaurant");
            }
        }


        private List<DishMenuItemViewModel> dishModels;

        public List<DishMenuItemViewModel> DishModels
        {
            get { return dishModels; }
            set
            {
                dishModels = value;
                this.RaisePropertyChanged("DishModels");
            }
        }

        public RelayCommand HandleOrderCommand { get; set; }

        public RelayCommand DishSelectedCommand { get; set; }

        public MainWindowViewModel()
        {
            this.LoadDishData();
            this.LoadRestaurant();
            this.HandleOrderCommand = new RelayCommand(new Action(HandleOrderAction));
            this.DishSelectedCommand = new RelayCommand(new Action(SelectedMednuItemAction));
        }

        private void LoadRestaurant()
        {
            Restaurant res = new Restaurant();
            res.Address = "银河太阳系地球中国安徽安庆";
            res.Name = "雪庭饭店";
            res.Telephone = "010-1258974";
            this.Restaurant = res;
        }

        private void LoadDishData()
        {
            List<Dish> listDish = SerializeService.Deserialize();
            this.DishModels = new List<DishMenuItemViewModel>();
            foreach (var item in listDish)
            {
                DishMenuItemViewModel viewModel = new DishMenuItemViewModel();
                viewModel.dish = item;
                this.DishModels.Add(viewModel);
            }
        }

        private void HandleOrderAction()
        {
            List<string> listOrder = DishModels.Where(n => n.IsSelected = true).Select(n => n.dish.Name).ToList();
            //IOrderService orderService = new MockOrderService();
            //orderService.HandleOrder(listOrder);
            MessageBox.Show("订单提交成功！");
        }

        private void SelectedMednuItemAction()
        {
            this.Count = this.DishModels.Count(n => n.IsSelected == true);
            this.Fee = this.DishModels.Where(n => n.IsSelected == true).Select(n => n.dish.Price).ToList().Sum(); 

        }
    }
}
